//
//  ToolInputView.m
//  BaseAPP
//
//  Created by Evisu on 17/7/14.
//  Copyright © 2017年 evisum. All rights reserved.
//

#import "ToolInputView.h"
#import "ShareEmotionView.h"

#define KTABVIEW_HEIGHT 222

@interface ToolInputView ()<UITextViewDelegate>{

    CGFloat _currentTextViewHeight;//记录输入框的高度
    CGFloat _kbHeight;//键盘高度
}

//顶部栏分割线
@property (nonatomic, strong)UIView *ivTopLine;

//中间分割线
@property (nonatomic, strong)UIView *ivCenterLine;



//表情扩展View
@property (nonatomic, strong)ShareEmotionView *shareEmotionView;

@end

@implementation ToolInputView

- (UIButton *)btnVoice {

    if (!_btnVoice) {
        
        _btnVoice = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    
    return _btnVoice;
}

- (UIButton *)btnEmotion {

    if (!_btnEmotion) {
        
        _btnEmotion = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    
    return _btnEmotion;
}

- (UIButton *)btnMore {

    if (!_btnMore) {
        
        _btnMore = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    
    return _btnMore;
}

- (UITextView *)tvInputView {

    if (!_tvInputView) {
        
        _tvInputView = [[UITextView alloc] init];
    }
    
    return _tvInputView;
}

- (UIView *)tabView {

    if (!_tabView) {
        _tabView = [[UIView alloc] init];
//        _tapView.backgroundColor = [UIColor blueColor];
    }
    
    return _tabView;
}

- (instancetype)init {

    if (self = [super init]) {
        
        [self initViews];
        [self layoutViews];
        
        [BaseNotificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    }
    
    return self;
}

- (void)initViews {

    self.backgroundColor = HEXColor(@"F4F4F6");
    
    self.ivTopLine = [[UIView alloc] init];
    self.ivTopLine.backgroundColor = HEXColor(@"D7D7D8");
    
    self.ivCenterLine = [[UIView alloc] init];
    self.ivCenterLine.backgroundColor = HEXColor(@"D7D7D8");
    
    [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoice"] forState:UIControlStateNormal];
    [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoiceHL"] forState:UIControlStateHighlighted];
    [self.btnVoice addTarget:self action:@selector(didClickedKeyboardOrVoice:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btnMore setBackgroundImage:[UIImage imageNamed:@"TypeSelectorBtn_Black"] forState:UIControlStateNormal];
    [self.btnMore setBackgroundImage:[UIImage imageNamed:@"TypeSelectorBtnHL_Black"] forState:UIControlStateHighlighted];
    [self.btnMore addTarget:self action:@selector(didClickedMore:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotion"] forState:UIControlStateNormal];
    [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotionHL"] forState:UIControlStateHighlighted];
    [self.btnEmotion addTarget:self action:@selector(didClickedKeyboardOrEmotion:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tvInputView.backgroundColor = HEXColor(@"FCFCFC");
    self.tvInputView.font = [UIFont systemFontOfSize:14.0];
    self.tvInputView.showsVerticalScrollIndicator = NO;
    [self.tvInputView.layer setMasksToBounds:YES];
    [self.tvInputView.layer setCornerRadius:6.0]; //设置矩圆角半径
    [self.tvInputView.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 221/255.0, 221/255.0, 221/255.0, 1 });
    [self.tvInputView.layer setBorderColor:colorref];//边框颜色
    CGColorRelease(colorref);
    CGColorSpaceRelease(colorSpace);
    self.tvInputView.returnKeyType = UIReturnKeySend;//发送按钮
    self.tvInputView.enablesReturnKeyAutomatically = YES;
    self.tvInputView.contentInset = UIEdgeInsetsZero;
    self.tvInputView.delegate = self;
    
    self.btnSpeaking = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnSpeaking.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
    [self.btnSpeaking.layer setMasksToBounds:YES];
    [self.btnSpeaking.layer setCornerRadius:6.0]; //设置矩圆角半径
    [self.btnSpeaking.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef colorSpace1 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref1 = CGColorCreate(colorSpace1,(CGFloat[]){ 184/255.0, 187/255.0, 190/255.0, 1 });
    [self.tvInputView.layer setBorderColor:colorref1];//边框颜色
    CGColorRelease(colorref1);
    CGColorSpaceRelease(colorSpace1);
    [self.btnSpeaking setTitle:@"按住 说话" forState:UIControlStateNormal];
    [self.btnSpeaking setTitle:@"松开 结束" forState:UIControlStateHighlighted];
    [self.btnSpeaking setTitleColor:HEXColor(@"565656") forState:UIControlStateNormal];
    [self.btnSpeaking setBackgroundImage:[UIImage imageWithColor:HEXColor(@"FCFCFC")] forState:UIControlStateNormal];
    [self.btnSpeaking setBackgroundImage:[UIImage imageWithColor:HEXColor(@"C6C7CA")] forState:UIControlStateHighlighted];
    self.btnSpeaking.hidden = YES;
    
    self.shareView = [[ShareScrollView alloc]init];
//    self.shareView.hidden = YES;
    self.shareView.shareModel = @[@[@"sharemore_pic", @"照片"], @[@"sharemore_video", @"拍摄"], @[@"sharemore_videovoip", @"视频聊天"], @[@"sharemore_location", @"位置"], @[@"红包"], @[@"sharemorePay", @"转账"], @[@"sharemore_friendcard", @"个人名片"], @[@"sharemore_myfav", @"收藏"], @[@"sharemore_wallet", @"卡券"]];

    self.shareEmotionView = [[ShareEmotionView alloc] init];
    
    [self addSubview:self.ivTopLine];
    [self addSubview:self.btnMore];
    [self addSubview:self.btnEmotion];
    [self addSubview:self.btnVoice];
    [self addSubview:self.tvInputView];
    [self addSubview:self.btnSpeaking];
    [self addSubview:self.tabView];
    
    [self.tabView addSubview:self.shareView];
    
    [self.tabView addSubview:self.shareEmotionView];
}

- (void)layoutViews {

    _currentTextViewHeight = 33;
    
    self.ivTopLine.sd_layout
    .topEqualToView(self)
    .leftEqualToView(self)
    .rightEqualToView(self)
    .heightIs(0.5);
    
    self.btnVoice.sd_layout
    .widthIs(35)
    .heightIs(35)
    .leftSpaceToView(self, 3)
    .bottomSpaceToView(self.tabView, 6);
    
    self.btnMore.sd_layout
    .widthIs(35)
    .heightEqualToWidth()
    .rightSpaceToView(self, 3)
    .bottomSpaceToView(self.tabView, 6);
    
    self.btnEmotion.sd_layout
    .widthIs(35)
    .heightIs(35)
    .rightSpaceToView(self.btnMore, 5)
    .bottomSpaceToView(self.tabView, 6);
    
    self.tvInputView.sd_layout
    .leftSpaceToView(self.btnVoice, 5)
    .rightSpaceToView(self.btnEmotion, 5)
    .heightIs(33)
    .bottomSpaceToView(self.tabView, 7);
    
    self.btnSpeaking.sd_layout
    .leftSpaceToView(self.btnVoice, 5)
    .rightSpaceToView(self.btnEmotion, 5)
    .heightIs(33)
    .bottomSpaceToView(self.tabView, 7);
    
    self.tabView.sd_layout
    .leftEqualToView(self)
    .rightEqualToView(self)
    .heightIs(0)
    .bottomEqualToView(self);
    
//    self.ivCenterLine.sd_layout
//    .topSpaceToView(self.tabView, 1)
//    .leftEqualToView(self.tabView)
//    .rightEqualToView(self.tabView)
//    .heightIs(0.5);
    
//    self.shareView.sd_layout
//    .topSpaceToView(self.ivCenterLine, 1)
//    .leftEqualToView(self.tabView)
//    .rightEqualToView(self.tabView)
//    .bottomEqualToView(self.tabView);

    self.shareView.sd_layout
    .heightIs(KTABVIEW_HEIGHT)
    .topSpaceToView(self.tabView,0)
    .leftEqualToView(self.tabView)
    .rightEqualToView(self.tabView);
    
    self.shareEmotionView.sd_layout
    .heightIs(KTABVIEW_HEIGHT)
    .topSpaceToView(self.tabView, 0)
    .leftEqualToView(self.tabView)
    .rightEqualToView(self.tabView);
    
}

#pragma mark textView代理
//点击发送
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if ([text isEqualToString:@"\n"]) {
        
        //监听点击“发送”按钮
        
        if ([self.delegate respondsToSelector:@selector(toolInputViewDidClickedSend:)]) {
            
            [self.delegate toolInputViewDidClickedSend:textView.text];
        }
        
        return NO;
    }
    
    return YES;
}

#pragma mark 输入文字时监听文字长度
- (void)textViewDidChange:(UITextView *)textView {
    
    self.tvInputView.contentInset = UIEdgeInsetsZero;
    
    CGSize size = [textView.text boundingRectWithSize:CGSizeMake(self.tvInputView.width_sd, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]} context:nil].size;
    
    UIFont *font = [UIFont systemFontOfSize:14.0];
    CGFloat lineHeight = [font lineHeight];
    CGFloat rows = size.height/lineHeight;

    if (rows<5) {
        //5行以内高度自适应
        
        [UIView animateWithDuration:0.25 animations:^{
            
            
            _currentTextViewHeight = self.tvInputView.contentSize.height;
            
            //底部输入View更新约束
            self.sd_layout.heightIs(self.tvInputView.contentSize.height+_kbHeight+16);
            self.tvInputView.sd_layout.heightIs(self.tvInputView.contentSize.height);
            
            
            [self.tvInputView updateLayout];
            [self updateLayout];
            [self layoutSubviews];
            
            if ([self.delegate respondsToSelector:@selector(toolInputViewEditing)]) {
                
                [self.delegate toolInputViewEditing];
            }
            
        }];
    } else {
        
        self.tvInputView.contentInset = UIEdgeInsetsMake(4, 0, 4, 0);
    }
}


#pragma mark 语音&文字 切换
- (void)didClickedKeyboardOrVoice:(UIButton *)btn {

    btn.selected = !btn.selected;
    
    if (btn.selected) {
        //点击语音
        self.btnSpeaking.hidden = NO;
        self.tvInputView.hidden = YES;
        
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewKeyboard"] forState:UIControlStateNormal];
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewKeyboardHL"] forState:UIControlStateHighlighted];
        
        self.btnEmotion.selected = NO;
        self.btnMore.selected = NO;
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotion"] forState:UIControlStateNormal];
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotionHL"] forState:UIControlStateHighlighted];
        
        //隐藏扩展View
        self.tabViewHeight = 0;

        [UIView animateWithDuration:0.25 animations:^{
            
            [self endEditing:YES];
        }];
//        self.tapView.backgroundColor = [UIColor blueColor];
        
    }else {
        //点击键盘
        self.btnSpeaking.hidden = YES;
        self.tvInputView.hidden = NO;
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoice"] forState:UIControlStateNormal];
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoiceHL"] forState:UIControlStateHighlighted];
        
        [self.tvInputView becomeFirstResponder];
    }
    
    if ([self.delegate respondsToSelector:@selector(toolInputViewDidClickedVoice:)]) {
        
        [self.delegate toolInputViewDidClickedVoice:self.btnVoice];
    }
}

#pragma mark 表情&键盘 切换
- (void)didClickedKeyboardOrEmotion:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    
    self.btnSpeaking.hidden = YES;
    self.tvInputView.hidden = NO;
    self.shareView.hidden = YES;
    self.shareEmotionView.hidden = NO;
    
    if (btn.selected) {
        //点击表情
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewKeyboard"] forState:UIControlStateNormal];
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewKeyboardHL"] forState:UIControlStateHighlighted];
        
        self.btnVoice.selected = NO;
        self.btnMore.selected = NO;
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoice"] forState:UIControlStateNormal];
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoiceHL"] forState:UIControlStateHighlighted];
        
        //展开表情View
        self.tabViewHeight = KTABVIEW_HEIGHT;
        [self endEditing:YES];
        self.tvInputView.sd_layout.heightIs(_currentTextViewHeight);
        [UIView animateWithDuration:0.25 animations:^{
            
            self.shareEmotionView.sd_layout.topSpaceToView(self.tabView,0);
            [self.shareEmotionView updateLayout];
        }];
        self.shareView.sd_layout.topSpaceToView(self.tabView, KTABVIEW_HEIGHT);
        [self.shareView updateLayout];

//        self.tapView.backgroundColor = [UIColor redColor];
        
    }else {
        //点击键盘
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotion"] forState:UIControlStateNormal];
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotionHL"] forState:UIControlStateHighlighted];
        
        [self.tvInputView becomeFirstResponder];
    }
    
    if ([self.delegate respondsToSelector:@selector(toolInputViewDidClickedEmotion:)]) {
        
        [self.delegate toolInputViewDidClickedEmotion:self.btnEmotion];
    }
}

#pragma mark 加号按钮 展开与关闭
- (void)didClickedMore:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    
    self.btnSpeaking.hidden = YES;
    self.tvInputView.hidden = NO;
    self.shareEmotionView.hidden = YES;
    self.shareView.hidden = NO;
    
    if (btn.selected) {
        //展开
        self.btnVoice.selected = NO;
        self.btnEmotion.selected = NO;
        
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoice"] forState:UIControlStateNormal];
        [self.btnVoice setBackgroundImage:[UIImage imageNamed:@"ToolViewInputVoiceHL"] forState:UIControlStateHighlighted];
        
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotion"] forState:UIControlStateNormal];
        [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotionHL"] forState:UIControlStateHighlighted];
        
        self.tabViewHeight = KTABVIEW_HEIGHT;
        self.tvInputView.sd_layout.heightIs(_currentTextViewHeight);
        [self endEditing:YES];
        
        [UIView animateWithDuration:0.25 animations:^{
            
            self.shareView.sd_layout.topSpaceToView(self.tabView,0);
            [self.shareView updateLayout];
        }];
        
        self.shareEmotionView.sd_layout.topSpaceToView(self.tabView, KTABVIEW_HEIGHT);
        [self.shareEmotionView updateLayout];
        
//        self.tabView.backgroundColor = [UIColor yellowColor];
        
    }else {
        //关闭
        [self.tvInputView becomeFirstResponder];
//        self.shareView.sd_layout.topSpaceToView(self.tabView, 0);
    }
    
    if ([self.delegate respondsToSelector:@selector(toolInputViewDidClickedMore:)]) {
        
        [self.delegate toolInputViewDidClickedMore:self.btnMore];
    }
    
}

#pragma mark - 键盘
#pragma mark 键盘弹出
- (void)keyboardWillShow:(NSNotification *)notification {

    NSValue *value = notification.userInfo[UIKeyboardFrameEndUserInfoKey];
    CGRect kbEndRect = [value CGRectValue];

    //键盘高度
    _kbHeight = kbEndRect.size.height+1;
    
    self.tvInputView.sd_layout.heightIs(_currentTextViewHeight);
    
    self.tabViewHeight = _kbHeight;
    
    self.btnEmotion.selected = NO;
    self.btnMore.selected = NO;
    [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotion"] forState:UIControlStateNormal];
    [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotionHL"] forState:UIControlStateHighlighted];
}

#pragma mark 设置 表情&加号 内容展示View的高度
- (void)setTabViewHeight:(CGFloat)tabViewHeight {

    _tabViewHeight = tabViewHeight;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.tabView.sd_layout.heightIs(tabViewHeight);
        if (self.btnVoice.selected) {
            
            self.sd_layout.heightIs(33+16);
        } else {
        
            self.sd_layout.heightIs(tabViewHeight+_currentTextViewHeight+16);
        }
        
        [self updateLayout];
        [self.tabView updateLayout];
    }];
    
}

- (void)showOriginalTextView {

    self.tvInputView.text = @"";
    [self textViewDidChange:self.tvInputView];
}

- (void)hideTabView {
    

    [UIView animateWithDuration:0.25 animations:^{
        
        self.tabViewHeight = 0;
        [self endEditing:YES];
        
    }];
    
    
    
    self.btnEmotion.selected = NO;
    self.btnMore.selected = NO;
    [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotion"] forState:UIControlStateNormal];
    [self.btnEmotion setBackgroundImage:[UIImage imageNamed:@"ToolViewEmotionHL"] forState:UIControlStateHighlighted];
    
    
}

- (void)dealloc {

    [BaseNotificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}


@end
